###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file produces figures for candidate choice, stratifying on individual covariates, gender, race, religion, etc
########################################################

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R 

rm(list=ls()[which(ls()!='dirs')])
library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/cces_stacked_unmatched.Rdata")


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

load("data/data_matrix_scored.Rdata")
candidate_matrix=data_matrix
#candidate_matrix=read.csv("csv/data_matrix_scored.csv",header=T,stringsAsFactors=F)[,-c(1)]

candidate_matrix[,which(names(candidate_matrix)=='race')[1]][which(candidate_matrix[,which(names(candidate_matrix)=='race')[1]]=='White')]='aWhite'
candidate_matrix[,which(names(candidate_matrix)=='religion')[1]][which(candidate_matrix[,which(names(candidate_matrix)=='religion')[1]]=='None Listed')]='aNone'

candidate_matrix[,which(names(candidate_matrix)=='religion')[1]][which(candidate_matrix[,which(names(candidate_matrix)=='religion')[1]]=='Evangelical Protestant')]='dEvangelical Protestant'
candidate_matrix[,which(names(candidate_matrix)=='religion')[1]][which(candidate_matrix[,which(names(candidate_matrix)=='religion')[1]]=='Protestant')]='bProtestant'
candidate_matrix[,which(names(candidate_matrix)=='religion')[1]][which(candidate_matrix[,which(names(candidate_matrix)=='religion')[1]]=='Catholic')]='cCatholic'

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

############################################################
############################################################
# CLUNKY CODE to replicate Figure 3 by stratification
############################################################
############################################################
### Primary Elections (by PID) || women

####

vote <- subset(candidate_matrix,candidate_matrix$conjoints==3|candidate_matrix$conjoints==8)
vote$pty <- ifelse(vote$conjoints==3,0,1)

reps <- subset(vote, vote$pid3==1)
dems <- subset(vote, vote$pid3==-1)
inds <- subset(vote, vote$pid3==0)

#dv_choice is the More Similar variable
# pty is included for independents here, otherwise of course NAs out.

attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(female==1),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(female==1),],primary_elec_reps,respondent[which(female==1)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(female==1),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(female==1),],primary_elec_dems,respondent[which(female==1)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(female==1),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(female==1),],primary_elec_inds,respondent[which(female==1)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$female==1)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$female==1)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_female_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

#female_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
#female_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,patter='Gender') | grepl(ggplot_stacked$labels,patter='Race') | grepl(ggplot_stacked$labels,patter='Religion') | grepl(ggplot_stacked$labels,patter='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_female_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)

############################################################
############################################################
### Primary Elections (by PID) || men

attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(male==1),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(male==1),],primary_elec_reps,respondent[which(male==1)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(male==1),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(male==1),],primary_elec_dems,respondent[which(male==1)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(male==1),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(male==1),],primary_elec_inds,respondent[which(male==1)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$male==1)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$male==1)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_male_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

male_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
male_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,patter='Gender') | grepl(ggplot_stacked$labels,patter='Race') | grepl(ggplot_stacked$labels,patter='Religion') | grepl(ggplot_stacked$labels,patter='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_male_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || "white"


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(white==1),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(white==1),],primary_elec_reps,respondent[which(white==1)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(white==1),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(white==1),],primary_elec_dems,respondent[which(white==1)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(white==1),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(white==1),],primary_elec_inds,respondent[which(white==1)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$white==1)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$white==1)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_white_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

white_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
white_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_white_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || "black"

attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(black==1),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(black==1),],primary_elec_reps,respondent[which(black==1)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(black==1),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(black==1),],primary_elec_dems,respondent[which(black==1)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(black==1),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(black==1),],primary_elec_inds,respondent[which(black==1)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$black==1)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$black==1)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_black_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-1.26,1.26) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

black_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
black_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_black_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-1.26,1.26) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)

############################################################
############################################################
### Primary Elections (by PID) ||  "hisps"

attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(hisps==1),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(hisps==1),],primary_elec_reps,respondent[which(hisps==1)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(hisps==1),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(hisps==1),],primary_elec_dems,respondent[which(hisps==1)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(hisps==1),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(hisps==1),],primary_elec_inds,respondent[which(hisps==1)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$hisps==1)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$hisps==1)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_hisps_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-1.26,1.26) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_hisps_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-1.26,1.26) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) ||  "religion_none"


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_none==1),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(religion_none==1),],primary_elec_reps,respondent[which(religion_none==1)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_none==1),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(religion_none==1),],primary_elec_dems,respondent[which(religion_none==1)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_none==1),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(religion_none==1),],primary_elec_inds,respondent[which(religion_none==1)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$religion_none==1)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$religion_none==1)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_religion_none_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_religion_none_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) ||  "religion_catholic"

attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_catholic==1),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(religion_catholic==1),],primary_elec_reps,respondent[which(religion_catholic==1)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_catholic==1),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(religion_catholic==1),],primary_elec_dems,respondent[which(religion_catholic==1)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_catholic==1),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(religion_catholic==1),],primary_elec_inds,respondent[which(religion_catholic==1)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$religion_catholic==1)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$religion_catholic==1)])),')'))
#

###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_religion_catholic_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_religion_catholic_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)



############################################################
############################################################
### Primary Elections (by PID) ||   "religion_protestant"

attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_protestant==1),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(religion_protestant==1),],primary_elec_reps,respondent[which(religion_protestant==1)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_protestant==1),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(religion_protestant==1),],primary_elec_dems,respondent[which(religion_protestant==1)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_protestant==1),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(religion_protestant==1),],primary_elec_inds,respondent[which(religion_protestant==1)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$religion_protestant==1)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$religion_protestant==1)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_religion_protestant_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_religion_protestant_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || "religion_evangelical"

attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_evangelical==1),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(religion_evangelical==1),],primary_elec_reps,respondent[which(religion_evangelical==1)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_evangelical==1),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(religion_evangelical==1),],primary_elec_dems,respondent[which(religion_evangelical==1)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(religion_evangelical==1),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(religion_evangelical==1),],primary_elec_inds,respondent[which(religion_evangelical==1)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$religion_evangelical==1)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$religion_evangelical==1)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_religion_evangelical_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_religion_evangelical_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)




############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_envs<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_envs<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_envs<=0),],primary_elec_reps,respondent[which(libcon_envs<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_envs<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_envs<=0),],primary_elec_dems,respondent[which(libcon_envs<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_envs<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_envs<=0),],primary_elec_inds,respondent[which(libcon_envs<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_envs<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_envs<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_envs_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_envs_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_envs>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_envs>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_envs>=0),],primary_elec_reps,respondent[which(libcon_envs>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_envs>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_envs>=0),],primary_elec_dems,respondent[which(libcon_envs>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_envs>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_envs>=0),],primary_elec_inds,respondent[which(libcon_envs>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_envs>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_envs>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_envs_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_envs_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_crme<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_crme<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_crme<=0),],primary_elec_reps,respondent[which(libcon_crme<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_crme<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_crme<=0),],primary_elec_dems,respondent[which(libcon_crme<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_crme<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_crme<=0),],primary_elec_inds,respondent[which(libcon_crme<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_crme<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_crme<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_crme_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_crme_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_crme>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_crme>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_crme>=0),],primary_elec_reps,respondent[which(libcon_crme>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_crme>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_crme>=0),],primary_elec_dems,respondent[which(libcon_crme>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_crme>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_crme>=0),],primary_elec_inds,respondent[which(libcon_crme>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_crme>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_crme>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_crme_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_crme_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_abrt<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_abrt<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_abrt<=0),],primary_elec_reps,respondent[which(libcon_abrt<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_abrt<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_abrt<=0),],primary_elec_dems,respondent[which(libcon_abrt<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_abrt<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_abrt<=0),],primary_elec_inds,respondent[which(libcon_abrt<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_abrt<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_abrt<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_abrt_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_abrt_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_abrt>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_abrt>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_abrt>=0),],primary_elec_reps,respondent[which(libcon_abrt>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_abrt>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_abrt>=0),],primary_elec_dems,respondent[which(libcon_abrt>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_abrt>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_abrt>=0),],primary_elec_inds,respondent[which(libcon_abrt>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_abrt>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_abrt>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_abrt_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_abrt_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_gays<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_gays<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_gays<=0),],primary_elec_reps,respondent[which(libcon_gays<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_gays<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_gays<=0),],primary_elec_dems,respondent[which(libcon_gays<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_gays<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_gays<=0),],primary_elec_inds,respondent[which(libcon_gays<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_gays<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_gays<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_gays_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_gays_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_gays>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_gays>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_gays>=0),],primary_elec_reps,respondent[which(libcon_gays>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_gays>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_gays>=0),],primary_elec_dems,respondent[which(libcon_gays>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_gays>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_gays>=0),],primary_elec_inds,respondent[which(libcon_gays>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_gays>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_gays>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_gays_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_gays_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_immi<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_immi<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_immi<=0),],primary_elec_reps,respondent[which(libcon_immi<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_immi<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_immi<=0),],primary_elec_dems,respondent[which(libcon_immi<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_immi<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_immi<=0),],primary_elec_inds,respondent[which(libcon_immi<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_immi<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_immi<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_immi_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_immi_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_immi>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_immi>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_immi>=0),],primary_elec_reps,respondent[which(libcon_immi>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_immi>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_immi>=0),],primary_elec_dems,respondent[which(libcon_immi>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_immi>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_immi>=0),],primary_elec_inds,respondent[which(libcon_immi>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_immi>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_immi>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_immi_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_immi_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)

############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_trad<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_trad<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_trad<=0),],primary_elec_reps,respondent[which(libcon_trad<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_trad<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_trad<=0),],primary_elec_dems,respondent[which(libcon_trad<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_trad<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_trad<=0),],primary_elec_inds,respondent[which(libcon_trad<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_trad<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_trad<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_trad_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_trad_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_trad>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_trad>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_trad>=0),],primary_elec_reps,respondent[which(libcon_trad>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_trad>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_trad>=0),],primary_elec_dems,respondent[which(libcon_trad>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_trad>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_trad>=0),],primary_elec_inds,respondent[which(libcon_trad>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_trad>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_trad>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_trad_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_trad_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_guns<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_guns<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_guns<=0),],primary_elec_reps,respondent[which(libcon_guns<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_guns<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_guns<=0),],primary_elec_dems,respondent[which(libcon_guns<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_guns<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_guns<=0),],primary_elec_inds,respondent[which(libcon_guns<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_guns<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_guns<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_guns_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_guns_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_guns>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_guns>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_guns>=0),],primary_elec_reps,respondent[which(libcon_guns>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_guns>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_guns>=0),],primary_elec_dems,respondent[which(libcon_guns>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_guns>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_guns>=0),],primary_elec_inds,respondent[which(libcon_guns>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_guns>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_guns>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_guns_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_guns_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_dfns<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_dfns<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_dfns<=0),],primary_elec_reps,respondent[which(libcon_dfns<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_dfns<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_dfns<=0),],primary_elec_dems,respondent[which(libcon_dfns<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_dfns<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_dfns<=0),],primary_elec_inds,respondent[which(libcon_dfns<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_dfns<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_dfns<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_dfns_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_dfns_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_dfns>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_dfns>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_dfns>=0),],primary_elec_reps,respondent[which(libcon_dfns>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_dfns>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_dfns>=0),],primary_elec_dems,respondent[which(libcon_dfns>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_dfns>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_dfns>=0),],primary_elec_inds,respondent[which(libcon_dfns>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_dfns>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_dfns>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_dfns_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_dfns_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_taxs<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_taxs<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_taxs<=0),],primary_elec_reps,respondent[which(libcon_taxs<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_taxs<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_taxs<=0),],primary_elec_dems,respondent[which(libcon_taxs<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_taxs<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_taxs<=0),],primary_elec_inds,respondent[which(libcon_taxs<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_taxs<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_taxs<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_taxs_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_taxs_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_taxs>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_taxs>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_taxs>=0),],primary_elec_reps,respondent[which(libcon_taxs>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_taxs>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_taxs>=0),],primary_elec_dems,respondent[which(libcon_taxs>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_taxs>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_taxs>=0),],primary_elec_inds,respondent[which(libcon_taxs>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_taxs>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_taxs>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_taxs_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_taxs_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || lib_env :: libcon_need<=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_need<=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_need<=0),],primary_elec_reps,respondent[which(libcon_need<=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_need<=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_need<=0),],primary_elec_dems,respondent[which(libcon_need<=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_need<=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_need<=0),],primary_elec_inds,respondent[which(libcon_need<=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_need<=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_need<=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_lib_need_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_lib_need_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)


############################################################
############################################################
### Primary Elections (by PID) || con_env :: libcon_need>=0


attach(reps)
primary_elec_reps <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_need>=0),weights=1/wt)

primary_elec_reps_clse <- clse.f(reps[which(libcon_need>=0),],primary_elec_reps,respondent[which(libcon_need>=0)])
detach(reps)

attach(dems)
primary_elec_dems <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_need>=0),weights=1/wt)

primary_elec_dems_clse <- clse.f(dems[which(libcon_need>=0),],primary_elec_dems,respondent[which(libcon_need>=0)])
detach(dems)

attach(inds)
primary_elec_inds <- lm(dv_choice~
	pty + g_female+
	re_black+re_hispanic+
	r_catholic+r_evangelical+r_protestant+
	o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
	p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
	e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
	rec_refuse+rec_secure+rec_stand+rec_work+
	i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
subset=which(libcon_need>=0),weights=1/wt)

primary_elec_inds_clse <- clse.f(inds[which(libcon_need>=0),],primary_elec_inds,respondent[which(libcon_need>=0)])
detach(inds)


#Now mashing it all together for ggplot

results_matrix <- cbind(primary_elec_dems$coefficients,primary_elec_inds$coefficients,primary_elec_reps$coefficients)
# remove pty coef
results_matrix=results_matrix[-c(2),]
results_matrix=cbind(results_matrix,NA,NA,NA)

for(j in 1:nrow(results_matrix)){
	ix=which(names(primary_elec_dems_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,4]=primary_elec_dems_clse[ix,2]
	}

	ix=which(names(primary_elec_inds_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,5]=primary_elec_inds_clse[ix,2]
	}

	ix=which(names(primary_elec_reps_clse[,2])==rownames(results_matrix)[j])
	if(length(ix)>0){
		results_matrix[j,6]=primary_elec_reps_clse[ix,2]
	}
}


results_matrix <- results_matrix[-c(1),c(1,4,2,5,3,6)]

g_male <- c("g_male",rep(0,6))
re_white <- c("re_white",rep(0,6))
r_none <- c("r_none",rep(0,6))
o_attorney <- c("o_attorney",rep(0,6))
p_decent <- c("p_decent",rep(0,6))
e_newspapers <- c("e_newspapers",rep(0,6))
rec_help <- c("rec_help",rep(0,6))
i_freetrade <- c("i1_freetrade",rep(0,6))


results_matrix=rbind(
	g_male[2:7],
	results_matrix[1,],
	re_white[2:7],
	results_matrix[2:3,],
	r_none[2:7],
	results_matrix[4:6,],
	o_attorney[2:7],
	results_matrix[7:15,],
	p_decent[2:7],
	results_matrix[16:22,],
	e_newspapers[2:7],
	results_matrix[23:34,],
	rec_help[2:7],
	results_matrix[35:38,],
	i_freetrade[2:7],
	results_matrix[39:nrow(results_matrix),]
)

colnames(results_matrix) <- c("d_estimate","d_se","i_estimate","i_se","r_estimate","r_se")
results_matrix <- results_matrix[1:nrow(results_matrix),]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,nrow(results_matrix),by=1)

#results_matrix_withnames[is.na(results_matrix_withnames)]<- 0
##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

full_matrix=results_matrix_withnames
rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

#core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='i_est')

o.order <- read.csv("data/core_for_ggplot_global.csv")$iv_order

core_for_ggplot=reOrder(x=core_for_ggplot,o=o.order)


core_for_ggplot[,1]=as.character(core_for_ggplot[,1])
#core_for_ggplot[,2]=as.character(core_for_ggplot[,2])
core_for_ggplot[,8]=as.character(core_for_ggplot[,8])
#core_for_ggplot=rbind(core_for_ggplot,c('i_ideology','Overall Ideology Rating',
	#primary_elec_dems_ideo_clse[2,1],primary_elec_dems_ideo_clse[2,2],
	#primary_elec_inds_ideo_clse[2,1],primary_elec_inds_ideo_clse[2,2],
	#primary_elec_reps_ideo_clse[2,1],primary_elec_reps_ideo_clse[2,2],
	#'Overall Ideology Rating'))

attach(core_for_ggplot)
labels=core_for_ggplot$labels


ggplot_stacked <- data.frame(
labels=c(as.character(labels), as.character(labels), as.character(labels)),
PID=c(rep("Democrat",65),rep("Independent",65),rep("Republican",65)),
estimate=c(d_estimate,i_estimate,r_estimate),
se=c(d_se,i_se,r_se))

#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

ggplot_stacked=ggplot_stacked[!grepl(ggplot_stacked$PID,pattern='Independent'),]
#ggplot_stacked$labels <- factor(ggplot_stacked$labels,levels=ggplot_stacked$labels[order(ggplot_stacked$labels)])

detach(core_for_ggplot)
attach(ggplot_stacked)


ggplot_stacked$estimate=as.numeric(as.character(ggplot_stacked$estimate))
ggplot_stacked$se=as.numeric(as.character(ggplot_stacked$se))

#exclude overall ideology
#ggplot_stacked=ggplot_stacked[-c(grep(as.character(ggplot_stacked[,1]),pattern='Overall')),]
ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
#write.csv(ggplot_stacked,"csv/choice_primary_ggplot_stacked.csv")
attach(ggplot_stacked)
labels=unique(ggplot_stacked$labels)

# choice_primary.R
primary_ggplot_stacked=ggplot_stacked

pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Democrat',
	replace=paste(sep='','Democrat (N=',length(unique(dems$respondent[which(dems$libcon_need>=0)])),')'))
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='Republican',
	replace=paste(sep='','Republican (N=',length(unique(reps$respondent[which(reps$libcon_need>=0)])),')'))
#


###########################################
###Then, plot those models

pdf("appendix/figures/choice_primary_twoparty_con_need_stratification.pdf", width=9, height=12, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()

hisps_D=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Democrat'),]
hisps_R=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Race') & grepl(ggplot_stacked$PID,pattern='Republican'),]
detach(ggplot_stacked)

rm(g_male,re_white,r_none,o_attorney,p_decent,e_newspapers,rec_help,i_freetrade,labels)

#shortstack
ggplot_stacked=ggplot_stacked[grepl(ggplot_stacked$labels,pattern='Gender') | grepl(ggplot_stacked$labels,pattern='Race') | grepl(ggplot_stacked$labels,pattern='Religion') | grepl(ggplot_stacked$labels,pattern='Issue'),]
labels=ggplot_stacked$labels=as.factor(as.character(ggplot_stacked$labels))
labels=unique(labels)

PID=ggplot_stacked$PID
estimate=ggplot_stacked$estimate
se=ggplot_stacked$se


attach(ggplot_stacked)
pd <- position_dodge(.5)
minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))

pdf("appendix/figures/choice_primary_twoparty_con_need_stratification_short.pdf", width=9, height=6, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=PID)) +
    scale_color_manual(values = c("blue4","firebrick1")) +
    scale_x_discrete(limits=rev(labels)) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
	 facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.56,0.56) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 	theme(legend.text = element_text(size = 10)) +
#    geom_vline(xintercept=0.5,size=0.5,linetype="dashed") +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=24.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=27.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	#geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Vote Choice)")
dev.off()
detach(ggplot_stacked)

#END
